﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.DD35.DbLoader.Views;
using System.Windows.Forms;

namespace AMS.DD35.DbLoader.Controllers
{
    abstract class EditorControllerBase : IBrowserController
    {
        protected BrowserDialog _browser;
        protected Form _editor;
        //protected ModelBase _model;

        public EditorControllerBase()
        {
            InitializeFields();
        }

        protected abstract void InitializeFields();

        protected abstract void LoadEditor();

        protected abstract void PrepNewEditor();

        #region BrowserController Methods
        public virtual void Edit()
        {
            if (_browser.SelectedItem != null)
            {
                // TODO: Get ClassData
                LoadEditor();
                _editor.ShowDialog();
            }
            else
            {
                MessageBox.Show("No selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public virtual void Accept()
        {
            if (_browser.SelectedItem != null)
            {
                // TODO: present selection to Master
                _browser.DialogResult = DialogResult.OK;
                _browser.Close();
            }
            else
            {
                MessageBox.Show("No selection", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public virtual void Cancel()
        {
            _browser.DialogResult = DialogResult.Cancel;
            _browser.Close();
        }

        public virtual void CreateNewItem()
        {
            PrepNewEditor();
            _editor.ShowDialog();
        }
        #endregion

        #region ControllerBase Methods
        public virtual void Execute()
        {
            // open browser
            _browser.ShowDialog();
        }

        public virtual void Save()
        {
            MessageBox.Show("Not Yet Implemented!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public virtual void Close()
        {
            // TODO: determine if cancelled without saving, quit without saving changes, or closed after saving changes

            if (false)//(_status == Status.Saved)
            {
                _editor.DialogResult = DialogResult.OK;
                _editor.Close();
            }
            else
            {
                DialogResult result = MessageBox.Show("Do you really want to quit without saving changes?",
                    "Unsaved Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.Yes)
                {
                    _editor.DialogResult = DialogResult.Cancel;
                    _editor.Close();
                }
            }
        }
        #endregion
    }
}
